
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.TemplateRights;
import com.crv.ci.service.ITemplateRightsService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class TemplateRightsAction extends AbstractAdminController<ITemplateRightsService>{

    @Autowired
    private ITemplateRightsService templateRightsService;
    
    @RequestMapping(value="/templaterights/list")
    public String list(TemplateRights templateRights,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(templateRights); 	
    	BizData4Page<TemplateRights> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("templateRights", templateRights);
    	return "/module/templaterights/list";
    }    
    
    @RequestMapping(value="/templaterights/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("templateRights", new TemplateRights());
        return "module/templaterights/edit";
    }    
    
    @RequestMapping(value="/templaterights/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TemplateRights templateRights = templateRightsService.fetch(id);
    	model.addAttribute("templateRights", templateRights);
        return "module/templaterights/edit";
    }    
    
    @RequestMapping(value="/templaterights/save")
    @ResponseBody
    public String save(HttpServletRequest request,TemplateRights templateRights){
		boolean isAddNew = StringUtils.isBlank(templateRights.getId())?true:false;
		if(isAddNew)
		{
			templateRights.setId(IdGenerator.createNewId());
			templateRightsService.add(templateRights);
		}
		else
		{
			templateRightsService.update(templateRights);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/templaterights/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	templateRightsService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(TemplateRights templateRights)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("userId", templateRights.getUserId());		
		conditions.put("templateId", templateRights.getTemplateId());		
		conditions.put("createDate", templateRights.getCreateDate());		
    	return conditions;
    }

    @Override
    protected ITemplateRightsService getMainService() {
        return templateRightsService;
    }

    @Override
    protected String getMainObjName() {
        return "templaterights";
    }

    @Override
    protected String getViewTitle() {
        return "templaterights";
    }
}
